package me.chan.util;


import me.chan.std.StdOut;

import java.util.Comparator;

/**
 * Created by JanGin on 2017/9/10.
 */
public class SortUtil {

    /** not be instantiated*/
    private SortUtil() {}

    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    boolean less(Comparator comparator, Object v, Object w) {
        return comparator.compare(v, w) < 0;
    }

    public static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

    public static boolean isSorted(Comparable[] a, int lo, int hi) {
        for (int i = lo + 1; i <= hi; i++)
            if (less(a[i], a[i-1])) return false;
        return true;
    }

    public static void show(Comparable[] a) {
        StdOut.print("[ ");
        for (int i = 0; i < a.length - 1; i++) {
            StdOut.print(a[i] + ", ");
        }
        StdOut.println(a[a.length - 1] + " ]");
    }
}
